//
//  NSECGRealTimeController.m
//  SpeedTest
//
//  Created by Han Mingjie on 2020/2/10.
//  Copyright © 2020 MingJie Han. All rights reserved.
//

#import "NSECGRealTimeController.h"
#import "pthread.h"

@interface NSECGRealTimeController(){
    pthread_mutex_t data_mutex;
    NSMutableArray <NSMutableArray <NSNumber *>*> *values_array_array;
}
@end

@implementation NSECGRealTimeController
@synthesize last_add_ecg_index;
@synthesize ecg_name_array;
@synthesize ecg_lead_status;
@synthesize number_for_maximum_count;
@synthesize drawed_ecg_index;

-(id)init{
    self = [super init];
    if (self){
        int ret = pthread_mutex_init(&data_mutex, nil);
        if (0 != ret){
            NSLog(@"*** Warning  the mutex can NOT init. ***");
        }
        last_add_ecg_index = 0;
    }
    return self;
}

-(void)setEcg_name_array:(NSArray<NSString *> *)_ecg_name_array{
    ecg_name_array = _ecg_name_array;
    pthread_mutex_lock(&data_mutex);
    values_array_array = [[NSMutableArray alloc] init];
    for (int i=0;i<ecg_name_array.count;i++){
        NSMutableArray *values_array = [[NSMutableArray alloc] init];
        [values_array_array addObject:values_array];
    }
    pthread_mutex_unlock(&data_mutex);
}

-(void)empty_all_ECG{
    pthread_mutex_lock(&data_mutex);
    for (NSMutableArray *values_array in values_array_array){
        [values_array removeAllObjects];
    }
    last_add_ecg_index = 0;
    drawed_ecg_index = 0;
    pthread_mutex_unlock(&data_mutex);
}

-(void)add_ECG_array:(NSArray <NSNumber *>*)from_array into:(NSMutableArray <NSNumber *>*)values_array{
    NSUInteger current_index = last_add_ecg_index;
    for (int i=0;i<from_array.count;i++){
        NSNumber *from_number = [from_array objectAtIndex:i];
        if (values_array.count <= current_index){
            //add object
            [values_array addObject:from_number];
        }else{
            //replace object
            [values_array replaceObjectAtIndex:current_index withObject:from_number];
        }
        current_index++;
        if (current_index >= number_for_maximum_count){
            current_index = 0;
        }
    }
    return;
}

-(BOOL)add_realtime_ECG_in_thread:(NSArray <NSArray <NSNumber *>*>* )ecg_array{
    if (ecg_array.count != values_array_array.count){
        NSLog(@"add ECG array count is NOT ecg_name_array.");
        return NO;
    }
    
    pthread_mutex_lock(&data_mutex);
    for (int i=0;i<ecg_array.count;i++){
        NSArray *a_array = [ecg_array objectAtIndex:i];
        NSMutableArray *values_array = [values_array_array objectAtIndex:i];
        [self add_ECG_array:a_array into:values_array];
    }
    last_add_ecg_index += ecg_array.firstObject.count;
    if (last_add_ecg_index >= number_for_maximum_count){
        last_add_ecg_index -= number_for_maximum_count;
    }
    pthread_mutex_unlock(&data_mutex);
    
    return YES;
}

-(BOOL)enough_point_for_next_piece:(NSUInteger)needed_points_number{
    pthread_mutex_lock(&data_mutex);
    if (last_add_ecg_index >= drawed_ecg_index){
        if (last_add_ecg_index - drawed_ecg_index > needed_points_number){
            pthread_mutex_unlock(&data_mutex);
            return YES;
        }else{
            pthread_mutex_unlock(&data_mutex);
            return NO;
        }
    }else{
        if ((last_add_ecg_index + number_for_maximum_count - drawed_ecg_index) > needed_points_number){
            pthread_mutex_unlock(&data_mutex);
            return YES;
        }else{
            pthread_mutex_unlock(&data_mutex);
            return NO;
        }
    }
}

-(void)one_piece_completed:(NSUInteger)points_number{
    pthread_mutex_lock(&data_mutex);
    drawed_ecg_index += points_number;
    if (drawed_ecg_index >= number_for_maximum_count){
        drawed_ecg_index -= number_for_maximum_count;
    }
    pthread_mutex_unlock(&data_mutex);
    return;
}

-(NSMutableArray <NSNumber *>*)getValueForLead:(NSString *)lead_name lenght:(NSUInteger)lenght status:(BOOL *)lead_status withDataIndex:(NSUInteger)data_index{
    NSMutableArray *result_array = [[NSMutableArray alloc] init];
    pthread_mutex_lock(&data_mutex);
    for (int i=0;i<ecg_name_array.count;i++){
        NSString *current_lead_name = [ecg_name_array objectAtIndex:i];
        if ([current_lead_name isEqualToString:lead_name]){
            NSMutableArray *from_lead_array = [values_array_array objectAtIndex:i];
            *lead_status = YES;
            if (ecg_lead_status){
                *lead_status = [[ecg_lead_status objectAtIndex:i] boolValue];
            }
            NSUInteger begin_index = data_index;
            for (int j=0;j<lenght;j++){
                NSNumber *number = [from_lead_array objectAtIndex:begin_index];
                [result_array addObject:number];
                begin_index++;
                if (begin_index >= number_for_maximum_count){
                    begin_index = 0;
                }
            }
            pthread_mutex_unlock(&data_mutex);
            return result_array;
        }
    }
    pthread_mutex_unlock(&data_mutex);
    return result_array;
}



@end
